<?php

namespace System\Models;

class DBText extends \System\Core\Model
{
    // Constants that match specific texts in the database table.
    const RULES         = 'RULES';
    const EMAILCONF     = 'EMAIL_CONFIRM';
    const EMAILRECOV    = 'EMAIL_RECOVER';
    const EMAILACCOUNT  = 'EMAIL_ACCOUNT';
    
    /**
     * Returns text from the database that equals the constants defined at
     * the top of this class.
     * 
     * @param string $type The name of the text.
     * @param array $data associative array to replace possible {<name>} keys 
     * in the text.
     * 
     * @return string The processed text from the database.
     */
    public function getText($content, $data = array())
    {
         $this->db->where('content', $content);
         $this->db->get('site_texts');
         if ($this->db->num_rows() == 1)
         {
             $text = $this->db->row()->text;
             
             // We have data to replace the {<name>} keys. See if they
             // exist in the text and replace them with the values in the 
             // associative arrary. 
             if (count($data) > 0)
             {
                 foreach ($data as $key => $value)
                 {
                     $text = preg_replace('/{'.$key.'}/i', $value, $text);
                 }
             }
             
             return $text;
         }
         else
         {
             throw new \System\Core\CommonException("The $type text is not available in the database.");
         }
    }
            
}

?>
